/**  
 * Copyrights@2006, Tianyuan DIC Information Co.,Ltd.
 * 文件名:    TaobaoResponseService.java  
 * 描述:   
 * 作者:     kaiyang
 * 版本:    1.0  
 * 创建时间:   2015年4月28日 下午9:27:28  
 *  
 * 修改历史:  
 * 日期                          作者           版本         描述  
 * ------------------------------------------------------------------  
 * 2015年4月28日      kaiyang      1.0        1.0 Version  
 */ 
package com.hp.bon.sgw.wh.service;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alipay.standard.util.SignUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;

/**  
 * @ClassName: TaobaoResponseService   
 * @Description: TODO  
 * @author: kaiyang  
 * @date:2015年4月28日 下午9:27:28     
 */
public class TaobaoResponseService {
	  private Configuration cfg = null;
	  private Map<String, Template> templateMap = new HashMap<String, Template>();
	  private static final Logger _log = LoggerFactory.getLogger(TaobaoResponseService.class);

	  public void init() throws IOException
	  {
	    this.cfg = new Configuration();
	    this.cfg.setClassForTemplateLoading(getClass(), "/");
	    this.cfg.setDefaultEncoding("UTF-8");
	    
	    Template templateError = this.cfg.getTemplate("taobaoError.ftl");
	    templateError.setEncoding("UTF-8");
	    this.templateMap.put("taobaoError", templateError);

	    Template templateTaobaoPay = this.cfg.getTemplate("taobaoPay.ftl");
	    templateTaobaoPay.setEncoding("UTF-8");
	    this.templateMap.put("taobaoPay", templateTaobaoPay);

	    Template templateTaobaoQueryFee = this.cfg.getTemplate("taobaoQueryFee.ftl");
	    templateTaobaoQueryFee.setEncoding("UTF-8");
	    this.templateMap.put("taobaoQueryFee", templateTaobaoQueryFee);
	  }

	  public String getResponse(String templateName, Map<String, String> data, String pathName, String password)
	  {
	    String context = "";
	    Template template = (Template)this.templateMap.get(templateName);
	    if (template != null)
	    {
	      StringWriter writer = new StringWriter();
	      try {
	        template.process(data, writer);
	      } catch (Exception e) {
	        _log.error("TaobaoResponseService getResponse error, msg = {}", e.getMessage());
	        e.printStackTrace();
	      }
	      context = writer.toString();
	    }
	    _log.info("签名前的内容:{}",context);
	    String signXml = SignUtil.sign(context,pathName,password);
	    _log.info("相应内容:{}",signXml);
	    return signXml;
	  }
}
